import { FC, useEffect } from "react";
import { QuestionTitlePropsType } from "./interface";
import { Checkbox, Form, Input, Select } from "antd";

const PropComponent: FC<QuestionTitlePropsType> = (
  props: QuestionTitlePropsType
) => {
  const { text, level, isCenter, disabled, onChange } = props;

  const [form] = Form.useForm();

  useEffect(() => {
    form.setFieldsValue({ text, isCenter, level });
  }, [text, isCenter, level, form]);

  function handleValuesChange() {
    // console.log(form.getFieldsValue());
    onChange?.(form.getFieldsValue());
  }

  return (
    <Form
      layout={"vertical"}
      initialValues={{ text, isCenter, level }}
      form={form}
      onValuesChange={handleValuesChange}
      disabled={disabled}
    >
      <Form.Item
        label="标题内容"
        name="text"
        rules={[{ required: true, message: "请输入标题内容" }]}
      >
        <Input></Input>
      </Form.Item>
      <Form.Item label="层级" name="level">
        <Select
          options={[
            { label: "1级标题", value: 1 },
            { label: "2级标题", value: 2 },
            { label: "3级标题", value: 3 },
          ]}
        ></Select>
      </Form.Item>
      <Form.Item label="是否居中" name="isCenter" valuePropName="checked">
        <Checkbox></Checkbox>
      </Form.Item>
    </Form>
  );
};

export default PropComponent;
